var utils = require('../../utils/util.js');
var base64Js = require('../../utils/base64.js');
var md5Js = require('../../utils/md5.js');
var app = getApp();
var object = {};
object.data = {};
Page({
  data:{
    orderHeader: [],
    orderBody: [],
    total: [],
  },
  onLoad:function(options){
     try{
        var orderid = options.id;
        var _this = this;
        //获取缓存的用户对象信息
        var user_info =  wx.getStorageSync('user_info');
        //如果用户信息不为空则请求首页接口获取产品等信息。
        if(user_info){
          var request_data = user_info.user_itmes;
          var uid         = request_data.id.toString();
          var private_key = user_info.private_key;
          var t           =  new Date().getTime().toString();
          var k           = md5Js.hex_md5(private_key+orderid+t);//key
          var parms_url   = app.request_url+'C_Order/'+orderid+'?';
          parms_url += '&uid='+request_data.id;
          parms_url += '&userType='+request_data.type;
          parms_url += '&t='+t;
          parms_url += '&k='+k;
          parms_url += '&v='+app.version;;
          parms_url += '&is_sku=1';
          // parms_url = 'http://rest.test.freshqiao.com/C_Order/3367?uid=100000202&userType=1&is_sku=1&t=1485064425088&k=1ce045984e0250cdb16ba668c50789b9&uid=100000202&v=2';
          //调用订单详情接口
          wx.request({
            url: parms_url,
            method: 'GET',
            success: function(res){
               var respones = res;
               var ordertotal = 0;
               var i;
               //请求成功分配模板变量
               if((respones.statusCode=='200')&&(respones.data.Success==1)){
                   //分配模板变量
                   for( i=0;i<res.data.CallInfo.orderBody.length;i++){
                     ordertotal += parseFloat(res.data.CallInfo.orderBody[i].total);//计算总价
                  }
                  _this.setData({orderHeader:res.data.CallInfo.orderHeader});
                  _this.setData({orderBody:res.data.CallInfo.orderBody});
                  _this.setData({total:ordertotal.toFixed(2)});
                  // 组装订单流程数据
                   var body_below = [];
                   var body_up = [];
                   var workflow_List = [];
                   for (var key=0;key<res.data.CallInfo.workflowList.length;key++){
                        res.data.CallInfo.workflowList[key]['time'] = res.data.CallInfo.workflowList[key]['time'].substr(0, 10);
                        if(key/2 == 0){
                            body_below.push(res.data.CallInfo.workflowList[key]);
                        }else{
                            body_up.push(res.data.CallInfo.workflowList[key]);
                        }
                   }
                   _this.setData({ orderBelow: body_below});
                   _this.setData({ orderUp: body_up });
                   _this.setData({ workflowList: res.data.CallInfo.workflowList });
               }
            }
          })
        }
    }catch(e){
      console.log('error=>'+e.message);return false;
    }
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  productinfo:function(e){
    var productinfo = e.currentTarget.dataset.productinfo;
    wx.navigateTo({
      url: '../shopInfo/shopInfo?productinfo='+JSON.stringify(productinfo),
    })
  }
})